<template>
  <div>
    <el-button type="danger" @click="batchDelete">批量删除</el-button>
    <el-tree
      :data="menus"
      :expand-on-click-node="false"
      node-key="catId"
      :default-expanded-keys="expandedKeys"
      :props="defaultProps"
      show-checkbox
      draggable
      :allow-drop="allowDrop"
      ref="menuTree"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >Append</el-button>
          <el-button type="text" size="mini" @click="() => edit(data)">Edit</el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >Delete</el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog :title="mode === 'add' ? '新增分类' : '编辑分类'" :visible.sync="dialogVisible" width="40%">
      <el-form ref="form" :model="category" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="category.name"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="category.icon"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input v-model="category.productUnit"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      expandedKeys: [],
      dialogVisible: false,
      mode: 'add',
      category: {
        catId: null,
        name: '',
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        icon: null,
        productUnit: null,
        productCount: 0
      }
    }
  },
  created() {
    this.getAllCategories()
  },
  methods: {
    getAllCategories() {
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({ data }) => {
        this.menus = data.data
      })
    },
    append(data) {
      // 给category模型赋值
      this.mode = 'add'
      this.category.catId = null
      this.category.parentCid = data.catId
      this.category.catLevel = data.catLevel * 1 + 1
      this.category.icon = null
      this.category.productUnit = null
      this.category.sort = 0
      this.category.showStatus = 1
      this.category.productCount = 0

      this.dialogVisible = true
    },
    edit(data) {
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: 'get'
      }).then(({ data }) => {
        this.category = data.category
        this.mode = 'edit'
        this.dialogVisible = true
      })
    },
    submit() {
      let url = null
      if (this.mode === 'add') {
        url = '/product/category/save'
      } else {
        url = '/product/category/update'
      }
      this.$http({
        url: this.$http.adornUrl(url),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: this.mode === 'add' ? '新增成功~' : '修改成功~',
            type: 'success'
          })

          this.dialogVisible = false
          this.getAllCategories()
          this.expandedKeys = [this.category.parentCid]
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    remove(node, data) {
      this.$confirm(`确定删除【${data.name}】吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const ids = [data.catId]

          this.$http({
            url: this.$http.adornUrl('/product/category/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '删除成功~',
                type: 'success'
              })

              // 重新加载数据
              this.getAllCategories()

              // 展开删除节点的父节点
              this.expandedKeys = [node.parent.data.catId]
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {})
    },
    allowDrop(draggingNode, dropNode, type) {
      return false
    },
    batchDelete() {
      const checkedKeys = this.$refs.menuTree.getCheckedKeys()
      console.log(checkedKeys)
    }
  }
}
</script>
